Registro los siguientes complementos en el arranque del servidor:
const registerPlugins = (app) => { app.register(require('fastify-cors'), { origin: '*', methods: ['POST', 'PUT', 'DELETE'], credentials: true, }); app.register(require('fastify-static'), { root: path.join(__dirname, 'client', 'build'), }); app.register(require('fastify-helmet'), { contentSecurityPolicy: false }); app.register(require('fastify-multer').contentParser); app.register(require('fastify-formbody')); app.register(require('fastify-qs'), {}); };Y tengo una ruta simple que se ve así:
fastify.post('/login', { handler, })Y mi controlador se ve así:
const handler = async (req, reply) => { try { const { email, password } = req.body; console.log('req.body', req.body); console.log('email', email); console.log('password', password); return reply.send(); } catch (err) { return reply.code(500).send({ error: err }); } }; el email y password no están definidos por alguna razón, pero cuando req.body , aparece esto:
req.body [Object: null prototype] { '{"email":"asdasd","password":"asdas"}': '' }¿Qué estoy haciendo mal aquí?
Hola Juan, yo tuve el mismo problema.
No encontré una manera limpia de deshacerme de él. Para evitar [Objeto: prototipo nulo] analicé nuevamente el cuerpo.
JSON.parse(JSON.stringify(req.body)) // => {"email":"asdasd","password":"asdas"}